package com.weed.helper;

import java.io.*;

/**
 * <p>Title: 调用Windows console程序</p>
 *
 * <p>Description:调用Windows console程序 </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author Jason zhuo
 * @version 1.0
 */
public abstract class WindowsHelper {

  /**
   * 调用Windows console程序
   * @param cmd
   * @return
   * @throws IOException
   */
  public static int run(String[] cmd, String workdirectory, StringBuffer resMsg) throws
      IOException {

    int nResult = -1; //0为成功
    if (resMsg != null) resMsg.setLength(0);
    try {
      ProcessBuilder builder = new ProcessBuilder(cmd);
      //set working directory
      if (workdirectory != null)
        builder.directory(new File(workdirectory));

      builder.redirectErrorStream(true);
      Process process = builder.start();
      InputStream in = process.getInputStream();
      InputStream es = process.getErrorStream();

      byte[] re = new byte[1024];
      int l = -1;
      while ( (l = in.read(re)) != -1) {
        System.out.print(new String(re, 0, l));
        if (resMsg != null) resMsg.append(new String(re, 0, l));
      }
      nResult = process.exitValue();
      in.close();
      es.close();
    }
    catch (Exception ex) {
      ex.printStackTrace();
      if (resMsg != null) resMsg.append("\n外部程序调用发生错误:" + ex.toString());
      nResult = -1;
    }
    return nResult;
  }

  public static int run(String[] cmd, String workdirectory, StringBuffer resMsg,
                        boolean wait) throws
      IOException {
    int nResult = -1; //0为成功
    if (resMsg != null) resMsg.setLength(0);
    try {
      ProcessBuilder builder = new ProcessBuilder(cmd);
      //set working directory
      if (workdirectory != null)
        builder.directory(new File(workdirectory));
      builder.redirectErrorStream(true);
      Process process = builder.start();
      if (wait) {
        InputStream in = process.getInputStream();
        InputStream es = process.getErrorStream();

        byte[] re = new byte[1024];
        int l = -1;
        while ( (l = in.read(re)) != -1) {
          System.out.print(new String(re, 0, l));
          if (resMsg != null) resMsg.append(new String(re, 0, l));
        }
        nResult = process.exitValue();
        in.close();
        es.close();
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
      if (resMsg != null) resMsg.append("\n外部程序调用发生错误:" + ex.toString());
      nResult = -1;
    }
    return nResult;
  }

  public static int run(String[] cmd, String workdirectory,
                        boolean wait) throws
      IOException {
    StringBuffer resMsg = new StringBuffer();
    int nResult = -1; //0为成功
    if (resMsg != null) resMsg.setLength(0);
    try {
      ProcessBuilder builder = new ProcessBuilder(cmd);
      //set working directory
      if (workdirectory != null)
        builder.directory(new File(workdirectory));
      builder.redirectErrorStream(true);
      Process process = builder.start();
      if (wait) {
        InputStream in = process.getInputStream();
        InputStream es = process.getErrorStream();

        byte[] re = new byte[1024];
        int l = -1;
        while ( (l = in.read(re)) != -1) {
          System.out.print(new String(re, 0, l));
          if (resMsg != null) resMsg.append(new String(re, 0, l));
        }
        nResult = process.exitValue();
        in.close();
        es.close();
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
      if (resMsg != null) resMsg.append("\n外部程序调用发生错误:" + ex.toString());
      nResult = -1;
    }
    return nResult;
  }

  public static int run(String[] cmd, String workdirectory) throws
      IOException {
    StringBuffer resMsg = new StringBuffer();
    int nResult = -1; //0为成功
    if (resMsg != null) resMsg.setLength(0);
    try {
      ProcessBuilder builder = new ProcessBuilder(cmd);
      //set working directory
      if (workdirectory != null)
        builder.directory(new File(workdirectory));
      builder.redirectErrorStream(true);
      Process process = builder.start();

      InputStream in = process.getInputStream();
      InputStream es = process.getErrorStream();

      byte[] re = new byte[1024];
      int l = -1;
      while ( (l = in.read(re)) != -1) {
        System.out.print(new String(re, 0, l));
        if (resMsg != null) resMsg.append(new String(re, 0, l));
      }
      nResult = process.exitValue();
      in.close();
      es.close();

    }
    catch (Exception ex) {
      ex.printStackTrace();
      if (resMsg != null) resMsg.append("\n外部程序调用发生错误:" + ex.toString());
      nResult = -1;
    }
    return nResult;
  }

  public static void main(String[] args) throws IOException {
    //ConsoleHelper.run(new String[] {"notepad"}, "c:\\", new StringBuffer(), false);
   //ConsoleHelper.run(new String[] {"notepad"}, "c:\\", new StringBuffer());
   // ConsoleHelper.run(new String[] {"notepad"}, "c:\\");
    WindowsHelper.run(new String[] {"cmd"}, "c:\\");
  }
}
